package com.cnswhy.cloud.server.system.controller;

import cn.hutool.core.convert.Convert;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.core.utils.BeanPlusUtil;
import com.cnswhy.cloud.common.core.utils.BizAssert;
import com.cnswhy.cloud.common.core.utils.TreeUtil;
import com.cnswhy.cloud.server.system.dto.SystemOrgPageDTO;
import com.cnswhy.cloud.server.system.dto.SystemOrgSaveDTO;
import com.cnswhy.cloud.server.system.dto.SystemOrgUpdateDTO;
import com.cnswhy.cloud.server.system.entity.SystemOrg;
import com.cnswhy.cloud.server.system.service.SystemOrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.cnswhy.cloud.common.core.utils.StrPool.*;

//import com.github.zuihou.security.annotation.PreAuth;


/**
 * <p>
 * 前端控制器
 * 组织
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-13
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/org")
@Api(value = "SystemOrg", tags = "组织")
//@PreAuth(replace = "systemOrg:")
public class SystemOrgController extends SuperController<SystemOrgService, Long, SystemOrg, SystemOrgPageDTO, SystemOrgSaveDTO, SystemOrgUpdateDTO> {

    @Override
    public R<SystemOrg> handlerSave(SystemOrgSaveDTO model) {
        SystemOrg org = BeanPlusUtil.toBean(model, SystemOrg.class);
        fillOrg(org);
        this.baseService.save(org);
        return success(org);
    }

    @Override
    public R<SystemOrg> handlerUpdate(SystemOrgUpdateDTO model) {
        SystemOrg org = BeanPlusUtil.toBean(model, SystemOrg.class);
        fillOrg(org);
        this.baseService.updateAllById(org);
        return success(org);
    }

    private SystemOrg fillOrg(SystemOrg org) {
        if (org.getParentId() == null || org.getParentId() <= 0) {
            org.setParentId(DEF_PARENT_ID);
            org.setTreePath(DEF_ROOT_PATH);
        } else {
            SystemOrg parent = this.baseService.getById(org.getParentId());
            BizAssert.notNull(parent, "父组织不能为空");

            org.setTreePath(StringUtils.join(parent.getTreePath(), parent.getId(), DEF_ROOT_PATH));
        }
        return org;
    }

    @Override
    public R<Boolean> handlerDelete(List<Long> ids) {
        return this.success(baseService.remove(ids));
    }


    /**
     * 查询系统所有的组织树
     *
     * @param status 状态
     * @return
     * @author zuihou
     * @date 2019-07-29 11:59
     */
    @ApiOperation(value = "查询系统所有的组织树", notes = "查询系统所有的组织树")
    @GetMapping("/tree")
    //@SysLog("查询系统所有的组织树")
    public R<List<SystemOrg>> tree(@RequestParam(value = "name", required = false) String name,
                             @RequestParam(value = "status", required = false) Boolean status) {
        List<SystemOrg> list = this.baseService.list(Wraps.<SystemOrg>lbQ()
                .like(SystemOrg::getLabel, name).eq(SystemOrg::getStatus, status).orderByAsc(SystemOrg::getSortValue));
        return this.success(TreeUtil.buildTree(list));
    }


    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<SystemOrg> userList = list.stream().map((map) -> {
            SystemOrg item = new SystemOrg();
            item.setDescribe(map.getOrDefault("描述", EMPTY));
            item.setLabel(map.getOrDefault("名称", EMPTY));
            item.setAbbreviation(map.getOrDefault("简称", EMPTY));
            item.setStatus(Convert.toBool(map.getOrDefault("状态", EMPTY)));
            return item;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(userList));
    }
}
